package com.yungu.swift.api.config;

import com.yungu.swift.api.base.gateway.CurrentUserHandlerMethodArgumentResolver;
import com.yungu.swift.api.base.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * @Description:
 * @author: gaozhenling
 * @date: 2019/03/22 17:14
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    private static final String[] EXCLUDE_PATH_PATTERNS = new String[]{
            "/api/v2/*/login",
            "/api/v2/*/refreshToken",
            "/api/v2/common/getConfig/*",
            "/api/v2/common/getDriRegCfg",
            "/api/v2/common/*/send",
            "/api/v2/common/*/tripSharing",
            "/api/v2/common/tag/list",
            "/api/v2/common/getPrivacyPolicy",
            "/api/v2/passenger/login/wx",
            "/api/v2/passenger/login/wx/applet/verify",
            "/api/v2/passenger/join/pointMatchedByAreaSiteCheck",
            "/api/v2/passenger/join/waysOriginByPas",
            "/api/v2/passenger/join/waysDestByPas",
            "/api/v2/passenger/join/maxPasSeats",
            "/api/v2/passenger/join/listCarLevel",
            "/api/v2/passenger/tag/problem",
            "/api/v2/passenger/tag/cancelMsg",
            "/api/v2/passenger/tag/takemsg",
            "/api/v2/passenger/version/check",
            "/api/v2/passenger/sysBusinessCity/list",
            "/api/v2/passenger/map/around",
            "/api/v2/driver/user/validate",
            "/api/v2/driver/user/verifyCode",
            "/api/v2/driver/user/reset"
    };

    private static final String[] EXCLUDE_V1_PATTERNS = new String[]{
            "/api/v2/passenger/common/getAirport",
            "/api/v2/passenger/common/token/uploadComplaintImg",
            "/api/v2/passenger/scanCodeGetDriverInfo",
            "/api/v2/passenger/token/getDeparTimeConfig",
            "/api/v2/passenger/order/create",
            "/api/v2/passenger/order/valuation",
            "/api/v2/passenger/order/getNoEvaluation",
            "/api/v2/passenger/token/join/order/parcel/evaluate",
            "/api/v2/passenger/token/join/order/parcel/create",
            "/api/v2/passenger/token/join/order/parcel/tag",
            "/api/v2/passenger/token/join/order/parcel/getReceiptCode",
    };

    @Autowired
    private LogHandler logHandler;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //服务器请求拦截器
        registry.addInterceptor(new ApiHandler())
                .addPathPatterns("/api/v1/**")
                .addPathPatterns(EXCLUDE_V1_PATTERNS)
                .excludePathPatterns("/api/v1/common/tripSharing")
                .excludePathPatterns("/api/v2/common/getDriRegCfg")
                .excludePathPatterns("/api/v1/driver/change/**");
        //司机端token拦截器
        registry.addInterceptor(new DriverTokenHandler())
                .addPathPatterns("/api/v1/driver/token/**")
                .addPathPatterns("/api/v1/common/driver/**")
                .addPathPatterns("/api/v2/driver/**")
                .excludePathPatterns("/api/v1/driver/token/marketing/isOpenCity")
                .excludePathPatterns("/api/v1/driver/change/**");
        //乘客端token拦截器
        registry.addInterceptor(new PassengerTokenHandler())
                .addPathPatterns("/api/v1/passenger/token/**")
                .addPathPatterns(EXCLUDE_V1_PATTERNS)
                .addPathPatterns("/wx/token/**")
                .excludePathPatterns("/api/v1/passenger/token/marketing/isOpenCity")
                .excludePathPatterns("/api/v2/passenger/scanCodeGetDriverInfo")
                .excludePathPatterns("/api/v2/passenger/token/getDeparTimeConfig")
                .excludePathPatterns("/api/v2/passenger/token/join/order/parcel/tag");
        /*v2 接口拦截器*/
        // 前置参数校验拦截器
        registry.addInterceptor(new PreHandler()).addPathPatterns("/api/v2/passenger/**")
                .excludePathPatterns(EXCLUDE_PATH_PATTERNS)
                .excludePathPatterns(EXCLUDE_V1_PATTERNS);
        // 防重放拦截器
        // registry.addInterceptor(new ReplayHandler()).addPathPatterns("/api/v2/passenger/**");
        // 权限拦截器
        registry.addInterceptor(new TokenHandler()).addPathPatterns("/api/v2/passenger/**")
                .excludePathPatterns(EXCLUDE_PATH_PATTERNS)
                .excludePathPatterns(EXCLUDE_V1_PATTERNS);
        // 签名拦截器
        registry.addInterceptor(new SignHandler()).addPathPatterns("/api/v2/passenger/**")
                .excludePathPatterns("/api/v2/upload/image")
                .excludePathPatterns(EXCLUDE_V1_PATTERNS);
        //日志跟踪
        registry.addInterceptor(logHandler);
        super.addInterceptors(registry);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/imgs/**").addResourceLocations("file:/home/admin/upload/imgs/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        //swagger配置
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("docs.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new CurrentUserHandlerMethodArgumentResolver());
    }
}
